<h1><%= Problem.model_name.human(count: 100) %></h1>

<%= render "filters" %>

<%= paginate @problems %>

<% if @problems.empty? %>
  <div class="alert alert-info">
    <%= t(".no_problems") %>
  </div>
<% end %>

<%= form_with url: resolve_problems_path, method: :post, class: "mb-3" do |form| %>
  <table class="table table-striped" data-controller="bulk-edit" data-action="change->bulk-edit#handleCheckboxChange">
    <tr>
      <th><input type="checkbox" aria-label="<%= translate ".select_all" %>" value="0" name="bulk-select-all"></th>
      <th colspan=2><%= t(".item") %></th>
      <th><%= t(".description") %></th>
      <th><span id="resolve-heading"><%= t(".resolve") %></span></th>
      <th><%= t(".ignore") %></th>
    </tr>
    <% @problems.each do |problem| %>
      <% next unless problem.problematic %>
      <tr id="problem-<%= problem.id %>" class="collapse show table-<%= problem_severity(problem) %> <%= problem.ignored && "opacity-50" %>">
        <%= turbo_stream_from problem %>
        <td><%= form.check_box "problems[#{problem.to_param}]", data: {resolution_strategy: problem.resolution_strategy, bulk_item: problem.to_param}, "aria-label": translate(".select") %></td>
        <% if problem.parent %>
          <td><%= link_to problem.parent.name, problem.parent, class: "link-dark" %></td>
          <% if problem.problematic.is_a? ModelFile %>
            <td><%= link_to problem.problematic.name, [problem.parent, problem.problematic], class: "link-dark" %></td>
          <% elsif problem.problematic.is_a? Link %>
            <td><%= link_to problem.problematic.url, problem.problematic.url, class: "link-dark" %></td>
          <% else %>
            <td></td>
          <% end %>
        <% else %>
          <td colspan=2><%= link_to problem.problematic.name, problem.problematic, class: "link-dark" %></td>
        <% end %>
        <td>
          <%= t "problems.%{type}.%{category}.title" % {type: problem.problematic_type.underscore, category: problem.category} %>
          <% if problem.note %>
            <br>
            <small><%= problem.note %></small>
          <% end %>
        </td>
        <td><%= ResolveButton(problem: problem, user: current_user) %></td>
        <td>
          <% if problem.ignored %>
            <%= link_to Icon(icon: "eye-fill", label: t(".unignore")), problem_path(problem, problem: {ignored: false}), method: :patch, class: "btn btn-secondary" %>
          <% else %>
            <%= link_to Icon(icon: "eye-slash", label: t(".ignore")), problem_path(problem, problem: {ignored: true}), method: :patch, class: "btn btn-secondary", data: {bs_toggle: "collapse", bs_target: "#problem-#{problem.id}"} %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>

  <%= form.submit translate(".bulk_resolve"), name: "resolve", class: "btn btn-danger", data: {confirm: translate(".bulk_confirm")} %>
  <%= form.submit translate(".bulk_ignore"), name: "ignore", class: "btn btn-secondary" %>
<% end %>

<%= paginate @problems %>
